package com.ofek.eventos;


import java.io.Serializable;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.ofek.eventos.ToolController.ToolListener;
import com.ofek.eventos.db.ContextDatabase;
import com.ofek.eventos.db.EventosDatabaseService;
import com.ofek.eventos.db.EventosSyncService;

public class EventosActivity extends FragmentActivity implements ToolListener {
	public static class ContextListFragment extends ListFragment {
		private ContextListAdapter listAdapter;
		@Override
		public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
				final Bundle savedInstanceState) {
			return inflater.inflate(R.layout.fragment_list, container,false);
		}
		@Override
		public void onActivityCreated(final Bundle savedInstanceState) {
			super.onActivityCreated(savedInstanceState);
			listAdapter = new ContextListAdapter(getActivity(),ContextDatabase.getInstance().getAll());
			getListView().setAdapter(listAdapter);
		}
		@Override
		public void onPause() {
			super.onPause();

			listAdapter.onPause();
		}
		@Override
		public void onResume() {
			super.onResume();
			listAdapter.clear();
			listAdapter.addAll(ContextDatabase.getInstance().getAll());
			listAdapter.onResume();
		}
		@Override
		public void onListItemClick(final ListView l, final View v, final int position, final long id) {
			super.onListItemClick(l, v, position, id);
			final Intent intent = new Intent(getActivity(),PageActivity.class);
			intent.putExtra(PageActivity.CONTEXT_DEFINER, (Serializable)l.getAdapter().getItem(position));
			startActivity(intent);
		}
	}


	@Override
	protected void onCreate(final Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_eventos);
		final ToolController toolController = new ToolController((ViewGroup) findViewById(R.id.toolView));
		toolController.registerToolListener(this);
		changeFrgment(new ContextListFragment());
		startService(new Intent(this,EventosDatabaseService.class));
		startService(new Intent(this,EventosSyncService.class));
	}
	protected void changeFrgment(final Fragment fragment) {
		final FragmentManager fragmentManager = getSupportFragmentManager();
		final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		fragmentTransaction.replace(R.id.container, fragment);
		fragmentTransaction.commit();
	}

	@Override
	public void onEditChecked(final boolean b) {
	}

	@Override
	public void onAddClicked() {
		EventosAndroidUtils.startFormActivity(this, FormActivity.ACTION_FORM_CONTEXT);
	}

	@Override
	public void onSearchClicked() {
		final Intent intent = new Intent(this, SearchActivity.class);
		startActivity(intent);
	}

	@Override
	public void onSyncClicked() { 
		LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent(EventosSyncService.REQUEST_SYNC));
	}

}
